package com.qingke.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class ValidateLoginFilter implements Filter {

    public ValidateLoginFilter() {
    	
    }

	public void destroy() {
		
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		System.out.println("validate");
		Object logged=((HttpServletRequest)request).getSession().getAttribute("logged");
		String uri=((HttpServletRequest)request).getRequestURI();
		System.out.println(uri);
		if(uri.endsWith(".html")){
			if(!uri.startsWith("/QSKPI/login.html")&&!uri.startsWith("/QSKPI/loginFail.html")){
				if((logged==null||!logged.equals("yes"))){
					request.setCharacterEncoding("utf-8");
					response.setCharacterEncoding("utf-8");
					System.out.println("no pass");
					response.getWriter().write("<script>alert('请先登录');window.location.href='login.html'</script>");
					return ;
				}
			}
		}
		System.out.println("pass");
		chain.doFilter(request, response);
	}

	public void init(FilterConfig fConfig) throws ServletException {
		
	}

}
